package com.example.demo.controller;

import com.example.demo.common.Func;
import com.example.demo.common.Result;
import com.example.demo.entity.mongodb.LogInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("mongodb")
@Api(tags = "17、MongoDB操作")
@ApiSupport(author = "罗海林", order = 17)
@Validated
@RequiredArgsConstructor
public class MongodbController {
    private final MongoTemplate mongoTemplate;

    @PostMapping("/insert")
    @ApiOperation(value = "插入数据", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 1)
    public Result insert() {
        LogInfo info = new LogInfo();
        info.setLogId(Func.getSnowID());
        info.setIp("192.168.1.105");
        info.setUrl("https://mp.weixin.qq.com/s/YPMARalQ4IRoNcexXT5JwQ");
        info.setMethod("POST");
        info.setParam("");
        info.setContentType("www-json");
        mongoTemplate.insert(info, "syslog");

        Result res = new Result();
        return res.ok();
    }

    @PostMapping("/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "logId", value = "id编号")
    })
    @ApiOperation(value = "更新数据", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 2)
    public Result update(@RequestParam String logId) {
        Query query = new Query(Criteria.where("logId").is(logId));
        Update update = new Update().set("method", "GET");

        UpdateResult result = mongoTemplate.updateMulti(query, update, LogInfo.class);

        Result res = new Result();
        res.ok();
        res.put("count", result.getMatchedCount());
        return res;
    }

    @PostMapping("/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "method", value = "方式")
    })
    @ApiOperation(value = "删除数据", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 3)
    public Result delete(@RequestParam String method) {
        Query query = new Query(Criteria.where("method").is(method));
        DeleteResult result = mongoTemplate.remove(query, LogInfo.class);

        Result res = new Result();
        res.ok();
        res.put("count", result.getDeletedCount());
        return res;
    }

    @PostMapping("/find")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "method", value = "方式")
    })
    @ApiOperation(value = "查询数据", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 4)
    public Result find(@RequestParam String method) {
        Query query = new Query(Criteria.where("method").is(method));
        List<LogInfo> list = mongoTemplate.find(query, LogInfo.class);

        Result res = new Result();
        res.ok();
        res.put("list", list);
        return res;
    }
}
